<!doctype html>

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<?php
include_once('navbar.php');
include_once('superheader.php');
?>

<script>
$(function() {
	
	$( "#subset" ).buttonset();
    $( "#datepicker" ).datepicker();
    $( "#datepicker2" ).datepicker();
    $( "#button5" ).button();
    $( "#button3" ).button();
    $("#response").html("");
        
		var params =  
		{
			colReq2: "requests",
		};

        var data = JSON.stringify(params);
         
        $.ajax({
                    cache: false,
                    type: "json",
                    url: "/phpfunctions/lists/listSoftware.php",
                    contentType: "application/json",
                    processData: true,
                    data: data,
             		success: function(res, textStatus, jqXHR){
		     var container = $("#response");
			 var table = $("<table border=\"1\">");
			 var head = $("<tr>");
			 <?php if($_SESSION['role'] === "admin" || $_SESSION['role'] === "support") : ?>
			 head.append("<th></th>");
			 <?php endif; ?>
			 head.append("<th>Request #</th>");
			 head.append("<th>Assignee</th>");
			 head.append("<th>Requester</th>");
			 head.append("<th>Description</th>");
			 head.append("<th>Submitted Date</th>");
			 head.append("<th>Scheduled Date</th>");
			 
		 	 table.append(head);

			 for(var key in res.data.messages)
			 {
				var tr = $("<tr>");
				var message = res.data.messages[key];
				var td = $("<td>");
				
			<?php if($_SESSION['role'] === "admin" || $_SESSION['role'] === "support") : ?>
			td = $("<td>");
				var link = $("<a href=\"editRequest.php?requestnum="+message.ticketNum+"\">Modify</a>")
				td.append(link)
				tr.append(td);
			<?php endif; ?>

				var td = $("<td>");
				td.html(message.ticketNum);
				tr.append(td);
				
				var td = $("<td>");
				td.html(message.assignee);
				tr.append(td);
					
				var td = $("<td>");
				td.html(message.requester);
				tr.append(td);
					
				var td = $("<td>");
				td.html(message.mDescription);
				tr.append(td);
					
				var td = $("<td>");
				td.html(message.subDate);
				tr.append(td);
					
				var td = $("<td>");
				td.html(message.schedDate);
				tr.append(td);
					
				table.append(tr);			
			 }

				container.html(table);
                    },
                    complete: function(XMLHttpRequest, text_status) {

                    }
                  });
});
</script>
</head>

<body>
	
	
	<div id="response" style="position: relative; top:10px; left:400px; height:0px">
      Stuff to be replaced
    </div>
	
	<h2>New Request:</h2>
	<?php if(isset($_GET["error"])) : ?>
		
		<?php if($_GET["error"] == 'emptyEquip') : ?>
		    <div class="ui-widget">
				<p>Please enter all valid maintenance request information.</p>
			</div>
		<?php endif; ?>
		
		<?php if($_GET["error"] == 'duplicateEquip') : ?>
		    <div class="ui-widget">
				<p>A maintenance request with that barcode already exists.</p>
			</div>
		<?php endif; ?>
		
		<?php if($_GET["error"] == 'dateEquip') : ?>
		    <div class="ui-widget">
				<p>You cannot schedule a maintenance for a date prior than today.</p>
			</div>
		<?php endif; ?>
		
	<?php endif; ?>
	
	<form action="phpfunctions/addrequest.php" method="post">
	 	<input type="hidden" name="username" value="<?=$_SESSION['username']?>" />
	 	<input type="hidden" name="assignee" value="<unassigned>" />
	 	<p>Maintenance Request Description</p>
	 	<textarea name="mDescription" cols="36" rows="7"></textarea>
	 	<p>Scheduled Date: <input type="text" name="schedDate" id="datepicker" />(click to pick date)</p>
	 	<p>Assignee: 
	 	<select name="assignee">
	 		<?php
		        $z = new Mongo();
		        $db = $z->EMU;
		        $collection = $db->users;
				$newCursor = $collection->find();

				foreach ($newCursor as $item) 
				{
					print "<option>";
					print $item['user'];
					print "</option>";
				}
			?>
	 	</select></p>
	 	
	 	<button id="button5" input type="submit" name="submit" value="Submit me!" />Submit</button>
	</form>
	<br>
	<h2>View Requests Created<br>by or Assigned to You:</h2>
	<div id="button3">
<a href="landing.php"><id="nav1" name="radio"><label for="nav1">Requests</label></a>
		<br/>
	</div>
	
	
</body>

</html>